package com.jch.ncud.view.chat;

import javafx.scene.control.Button;
import javafx.scene.layout.Pane;

// Chat窗口事件定义类
public class ChatEventDefine {

    private ChatInit chatInit;

    public ChatEventDefine(ChatInit chatInit) {
        this.chatInit = chatInit;

        chatInit.move();
        this.barChat();
        this.barFriend();
    }

    // 聊天按钮
    private void barChat() {
        Button barChat = chatInit.$("bar_chat", Button.class); // 聊天按钮
        Pane groupBarChat = chatInit.$("group_bar_chat", Pane.class); // 聊天面板
        // 事件触发 点击按钮
        barChat.setOnAction(event -> {
            switchBarChat(barChat, groupBarChat, true);// 切换按钮和面板状态
            switchBarFriend(chatInit.$("bar_friend", Button.class), chatInit.$("group_bar_friend", Pane.class), false);
        });
        // 鼠标输入 光标进入按钮区域
        barChat.setOnMouseEntered(event -> {
            boolean visible = groupBarChat.isVisible();
            if (visible) return;
            barChat.setStyle("-fx-background-image:url('/fxml/chat/img/system/chat_1.png')");
        });
        // 鼠标退出 光标移出按钮区域
        barChat.setOnMouseExited(event -> {
            boolean visible = groupBarChat.isVisible();
            if (visible) return;
            barChat.setStyle("-fx-background-image: url('/fxml/chat/img/system/chat_0.png')");
        });

    }

    // 好友按钮事件
    private void barFriend(){
        Button barFriend = chatInit.$("bar_friend",Button.class);
        Pane groupBarFriend = chatInit.$("group_bar_friend",Pane.class);
        barFriend.setOnAction(event->{
            switchBarChat(chatInit.$("bar_chat",Button.class),chatInit.$("group_bar_chat",Pane.class),false);
            switchBarFriend(barFriend,groupBarFriend,true);
        });
        barFriend.setOnMouseEntered(event->{
            boolean visible = groupBarFriend.isVisible();
            if (visible) return;
            barFriend.setStyle("-fx-background-image: url('/fxml/chat/img/system/friend_1.png')");
        });
        barFriend.setOnMouseExited(event -> {
            boolean visible = groupBarFriend.isVisible();
            if (visible) return;
            barFriend.setStyle("-fx-background-image:url('/fxml/chat/img/system/friend_0.png')");
        });
    }

    // 点击bar_friend按钮切换按钮背景图及关联背景版
    private void switchBarFriend(Button bar_friend, Pane group_bar_friend, boolean toggle) {
        if (toggle) {
            bar_friend.setStyle("-fx-background-image:url('/fxml/chat/img/system/friend_2.png')");
            group_bar_friend.setVisible(true);
        } else {
            bar_friend.setStyle("-fx-background-image:url('/fxml/chat/img/system/friend_0.png')");
            group_bar_friend.setVisible(false);
        }
    }

    // 点击bar_chat 按钮切换按钮背景图以及关联面板的可见性
    private void switchBarChat(Button barChat, Pane groupBarChat, boolean toggle) {
        if (toggle) {
            // 设置按钮背景图
            barChat.setStyle("-fx-backgroud-image:url('/fxml/chat/img/system/chat_2.png')");
            groupBarChat.setVisible(true); // 设置可见性
        } else {
            barChat.setStyle("-fx-background-image:url('/fxml/chat/img/system/chat_0.png')");
            groupBarChat.setVisible(false);
        }
    }
}
